package com.qf.mybatisplus.utils;


import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.env.Environment;
import org.springframework.stereotype.Component;

/**
 * 读取配置文件工具类
 *
 * @Author: xgh
 */
@Component
@SuppressWarnings("unused")
public class EnvUtils {
    private final static Logger logger = LoggerFactory.getLogger(EnvUtils.class);
    private static Environment environment;


    private EnvUtils() {
    }

    @Autowired
    public void setEnv(Environment env) {
        environment = env;
    }

    /**
     * 根据key去寻找对应的值
     */
    public static String getProperty(String key) {
        String result = environment.getProperty(key);
        logger.info("key为：{},的配置文件值为:{}", key, result);
        return result;
    }

    /**
     * 根据key去寻找对应的值并转换成指定类型
     */
    public static <T> T getProperty(String key, Class<T> targetType) {
        T result = environment.getProperty(key, targetType);
        logger.info("key为：{},的配置文件值为:{}", key, result);
        return result;
    }


}
